#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import sys
from dotenv import load_dotenv

# 先加载环境变量（必须在导入配置前）
load_dotenv()

def main():
    """主函数"""
    try:
        # 导入应用
        from app import create_app
        
        # 获取环境配置
        config_name = os.getenv('FLASK_ENV', 'development')
        
        # 创建应用实例
        app = create_app(config_name)
        
        # 获取运行参数
        host = os.getenv('HOST', '0.0.0.0')
        port = int(os.getenv('PORT', 5000))
        debug = os.getenv('FLASK_ENV') == 'development'
        
        print(f"🚀 启动体育场馆预约系统...")
        print(f"📍 环境: {config_name}")
        print(f"🌐 地址: http://{host}:{port}")
        print(f"📚 API文档: http://{host}:{port}/apidocs/")
        print(f"❤️ 健康检查: http://{host}:{port}/health")
        
        # 启动应用
        app.run(
            host=host, 
            port=port, 
            debug=debug,
            use_reloader=debug
        )
        
    except ImportError as e:
        print(f"❌ 导入错误: {e}")
        print("请检查依赖是否正确安装: pip install -r requirements.txt")
        sys.exit(1)
    except Exception as e:
        print(f"❌ 启动失败: {e}")
        sys.exit(1)

if __name__ == '__main__':
    main()

# 让 Gunicorn 能找到 app 实例
from app import create_app
import os
app = create_app(os.getenv('FLASK_ENV', 'production')) 